package cn.cnpc.springmvc4.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * 同过@ContorllerAdvice配置全局变量
 * @author ShowBest
 *
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {
	@ExceptionHandler(value=Exception.class)//在此处定义全局变量，拦截所有exception
 public ModelAndView exception(Exception exception,WebRequest webrequest){
		ModelAndView view = new ModelAndView("error");//error页面
		view.addObject("errorMessage",exception.getMessage()+"2344");
		return view;
	}
	@ModelAttribute//此注解将键值对添加到全局，所有注解了@RequestMapping的方法可获得此键值对
	public void addAttributes(Model model){
		model.addAttribute("msg", "额外信息");
	}
	
	@InitBinder //注解定制WebDataBinder
	public void initBinder(WebDataBinder webDataBinder){
		webDataBinder.setDisallowedFields("id");
	}
}
